package com.ejlchina.tool.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.adolf.androiddemo.databinding.ItemBottomSheetListBinding

class BottomSheetListAdapter(
    private val dataList: List<String>,
    private val itemClickListener: (Int,String) -> Unit
) : RecyclerView.Adapter<BottomSheetListAdapter.BottomSheetListViewHolder>() {

    private var selectIndex = -1
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BottomSheetListViewHolder {
        val binding = ItemBottomSheetListBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return BottomSheetListViewHolder(binding,itemClickListener)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: BottomSheetListViewHolder, position: Int) {
        holder.bind(dataList[position], position)
    }

    inner class BottomSheetListViewHolder(private val binding: ItemBottomSheetListBinding,private val itemClickListener: (Int,String) -> Unit) : RecyclerView.ViewHolder(binding.root) {
        fun bind(data: String, position: Int) {
            binding.tvContent.text = data
            binding.ivSelect.visibility = if (position == selectIndex) View.VISIBLE else View.GONE
            binding.root.setOnClickListener {
                selectIndex = position
                notifyDataSetChanged()
                itemClickListener(position,data) }
        }
    }
}